home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / MAZE_1.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  4KB  |  187 lines

  1. /* +++Date last modified: 02-Nov-1995 */
  2.  
  3. /*
  4.    This program makes 10x10 mazes and prints them on the screen.  No
  5.    promise of portability is made, but it does seem to work on NS GNX
  6.    C.
  7.  
  8.    Public Domain by Jonathan Guthrie.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14.  
  15. #define UP  1
  16. #define DN  2
  17. #define LT  4
  18. #define RT  8
  19.  
  20. int  addelem(int, int [12][12], int *, int, int);
  21. void openwall(int [12][12], int, int);
  22. void writemaze(int [12][12]);
  23.  
  24. main()
  25. {
  26.       int i, j, base;
  27.       int search[150], array[12][12];
  28.  
  29.       for(i=1 ; i<11 ; ++i)
  30.       {
  31.             array[i][0] = -1;
  32.             array[i][11] = -1;
  33.             array[0][i] = -1;
  34.             array[11][i] = -1;
  35.             for(j=1 ; j<11 ; ++j)
  36.                   array[i][j] = 0;
  37.       }
  38.  
  39.       srand((int)time(NULL));
  40.       i = rand() % 10 + 1;
  41.       j = rand() % 10 + 1;
  42.       base = addelem(0, array, search, i, j);
  43.       array[i][j] = RT + RT;      /* Not a valid value */
  44.       while(0 < base)
  45.       {
  46.             i = rand() % base;
  47.             j = search[i];
  48.             search[i] = search[--base];
  49.             i = j % 100;
  50.             j /= 100;
  51.             openwall(array, i, j);
  52.             base = addelem(base, array, search, i, j);
  53.       }
  54.  
  55.       writemaze(array);
  56.       return 0;
  57. }
  58.  
  59.  
  60. int addelem(int base, int maze[12][12], int *search, int row, int col)
  61. {
  62.       if(0 == maze[row-1][col])
  63.       {
  64.             search[base++] = row + col * 100 - 1;
  65.             maze[row-1][col] = -DN;
  66.       }
  67.       else if(0 > maze[row-1][col])
  68.             maze[row-1][col] -= DN;
  69.  
  70.       if(0 == maze[row+1][col])
  71.       {
  72.             search[base++] = row + col * 100 + 1;
  73.             maze[row+1][col] = -UP;
  74.       }
  75.       else if(0 > maze[row+1][col])
  76.             maze[row+1][col] -= UP;
  77.  
  78.       if(0 == maze[row][col-1])
  79.       {
  80.             search[base++] = row + col * 100 - 100;
  81.             maze[row][col-1] = -RT;
  82.       }
  83.       else if(0 > maze[row][col-1])
  84.             maze[row][col-1] -= RT;
  85.  
  86.       if(0 == maze[row][col+1])
  87.       {
  88.             search[base++] = row + col * 100 + 100;
  89.             maze[row][col+1] = -LT;
  90.       }
  91.       else if(0 > maze[row][col+1])
  92.             maze[row][col+1] -= LT;
  93.  
  94.       return base;
  95. }
  96.  
  97.  
  98. void openwall(int maze[12][12], int row, int col)
  99. {
  100.       int directions, max, direction, temprow, tempcol, temp, back;
  101.  
  102.       directions = -maze[row][col];
  103.  
  104.       max = 0;
  105.       if(directions & UP)
  106.       {
  107.             temp = rand();
  108.             if(temp > max)
  109.             {
  110.                   max = temp;
  111.                   direction = UP;
  112.                   back = DN;
  113.                   temprow = row - 1;
  114.                   tempcol = col;
  115.             }           
  116.       }
  117.  
  118.       if(directions & DN)
  119.       {
  120.             temp = rand();
  121.             if(temp > max)
  122.             {
  123.                   max = temp;
  124.                   direction = DN;
  125.                   back = UP;
  126.                   temprow = row + 1;
  127.                   tempcol = col;
  128.             }
  129.       }
  130.  
  131.       if(directions & LT)
  132.       {
  133.             temp = rand();
  134.             if(temp > max)
  135.             {
  136.                   max = temp;
  137.                   direction = LT;
  138.                   back = RT;
  139.                   temprow = row;
  140.                   tempcol = col - 1;
  141.             }
  142.       }
  143.  
  144.       if(directions & RT)
  145.       {
  146.             temp = rand();
  147.             if(temp > max)
  148.             {
  149.                   max = temp;
  150.                   direction = RT;
  151.                   back = LT;
  152.                   temprow = row;
  153.                   tempcol = col + 1;
  154.             }
  155.       }
  156.     
  157.       maze[row][col] = direction;
  158.       maze[temprow][tempcol] += back;
  159. }
  160.  
  161. void writemaze(int maze[12][12])
  162. {
  163.       int i, j;
  164.  
  165.       puts("*********************");
  166.       for(i=1 ; i<11 ; ++i)
  167.       {
  168.             putchar('*');
  169.             for(j=1 ; j<11 ; ++j)
  170.             {
  171.                   putchar(' ');
  172.                   if(maze[i][j] & RT)
  173.                         putchar(' ');
  174.                   else  putchar('*');
  175.             }
  176.             putchar('\n');
  177.             for(j=1 ; j<11 ; ++j)
  178.             {
  179.                   putchar('*');
  180.                   if(maze[i][j] & DN)
  181.                         putchar(' ');
  182.                   else  putchar('*');
  183.             }
  184.             puts("*");
  185.       }
  186. }
  187.